<?php

class Controller_Manager_Link extends Controller
{
    public function __construct()
    {
        $aUser = getCurrentUser();
        if (!isset($aUser['utype']) || $aUser['utype'] != 'manager')
        {
            Controller_Error::forbidden();
        }
    }

    public function index()
    {
        $id = isset($this->params[0]) ? intval($this->params[0]) : 0;
        $banner = new Model_Banner($id);
        $aBanner = $banner->values;



        $this->view->add(array(
            'id'      => $id,
            'aBanner' => $aBanner,
        ));
        return $this->view->render('manager/link/index.tpl');
    }

    public function edit()
    {
        $iBannerId = isset($this->params[0]) ? intval($this->params[0]) : 0;
        $iLinkId = isset($this->params[1]) ? intval($this->params[1]) : 0;
        $link = new Model_Link($iLinkId);
        $banner = new Model_Banner($iBannerId);
        $aBanner = $banner->values;

        $aSites = DB::hashedRows('SELECT * FROM sites');
        //$aBanners =

        if ($aBanner['start_date'])
        {
            $aData['start_date'] = date('d.m.Y H:i', $aBanner['start_date']);
        }
        if ($aBanner['end_date'])
        {
            $aData['end_date'] = date('d.m.Y H:i', $aBanner['end_date']);
        }

        $errors = array();
        if (Request::isPost())
        {
            $aData = Request::postVars(array(
                'site_id',
                'place_id',
                'start_date',
                'end_date',
                'show_only_on_main',
                'pages',
                'zones',
                'clicks_limit',
                'views_limit',
                'daily_clicks_limit',
                'daily_views_limit',
                'host_limit',
                'traffic_limit',
                'avg_visits',
                'priority',
            ));
            $validator = new Validator();
            $sCommonErrorMsg = 'Корректно заполните поле';
            $validator->add(new Rule_InArray('site_id', array_keys($aSites), 'Выберите сайт'));
            $validator->add(new Rule_IntegerPositive('place_id', 'Выберите место'));
            $validator->add(new Rule_DatePeriod('start_date', 'end_date', 'Введите дату начала'));
            $validator->add(new Rule_NotEmpty('end_date', 'Введите дату окончания'));
            $validator->add(new Rule_Number('clicks_limit', $sCommonErrorMsg, 0));
            $validator->add(new Rule_Number('views_limit', $sCommonErrorMsg, 0));
            $validator->add(new Rule_Number('daily_clicks_limit', $sCommonErrorMsg, 0));
            $validator->add(new Rule_Number('daily_views_limit', $sCommonErrorMsg, 0));
            $validator->add(new Rule_Number('host_limit', $sCommonErrorMsg, 0));
            $validator->add(new Rule_Number('traffic_limit', $sCommonErrorMsg, 0));
            $validator->add(new Rule_Number('avg_visits', $sCommonErrorMsg, 0));
            $validator->add(new Rule_Number('priority', 'Выберите приоритет', 0));

            if ($validator->validate($aData))
            {
                $aData['banner_id'] = intval($iBannerId);
                $aData['start_date'] = strtotime($aData['start_date']);
                $aData['end_date']   = strtotime($aData['end_date']);
                if ($aData['show_only_on_main'])
                {
                    $aData['pages'] = '/';
                }
                unset($aData['show_only_on_main']);
                if ($iLinkId)
                {
                    $link->save($aData);
                    Redirect('/manager/link/'.$iBannerId);
                }
                else
                {
                    $aData['cdate'] = time();
                    $link->values = $aData;
                    $link->save();
                    if ($link->id)
                    {
                        Redirect('/manager/link/'.$iBannerId);
                    }
                }
            }
            else
            {
                $errors = $validator->errors;
            }
        }

        $aPriority = array(
            '1' => 'Самый высокий приоритет',
        );

        $this->view->add(array(
            'iLinkId'   => $iLinkId,
            'iBannerId' => $iBannerId,
            'aData'     => $aData,
            'aBanner'   => $aBanner,
            'aSites'    => $aSites,
            'aPriority' => $aPriority,
            'errors'    => $errors,
        ));
        return $this->view->render('manager/link/edit.tpl');
    }

    public function getSitePlacesList()
    {
        $SiteId = isset($this->params[0]) ? intval($this->params[0]) : 0;
        $aPlaces = DB::hashedRows('SELECT * FROM places WHERE site_id = ?i', $SiteId);
        $aResponce = array(
            'success' => true,
            'data'    => $aPlaces,
        );
        die(json_encode($aResponce));
    }
}